﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace PriceImporter
{
    public class PriceProduct
    {
        public string Reference { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public bool Availability { get; set; }

        public PriceProduct(string rawProduct)
        {
            var parametersSplitter = new[] { ";" };
            var trimmers = new [] {' '};
            string[] parameters = rawProduct.Split(parametersSplitter,StringSplitOptions.None);

            try
            {
                string firstWord = parameters[0].Split(trimmers)[0];
                if (Regex.IsMatch(firstWord, @"\d"))
                {
                    Reference = firstWord.TrimEnd(trimmers).TrimStart(trimmers);
                    Name = parameters[0].TrimStart(firstWord.ToCharArray()).TrimEnd(trimmers).TrimStart(trimmers);
                    Price = Convert.ToDecimal(parameters[2]);
                    int number;
                    Int32.TryParse(parameters[3], out number);
                    if(number > 0)
                    Availability = true;
                }
            }
            catch {}
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            string[] productSplitter = new []{"\r\n"};
            string text = File.ReadAllText(@"D:\\price.csv", Encoding.GetEncoding(1251));
            if(!string.IsNullOrEmpty(text))
            {
                string[] products = text.Split(productSplitter, StringSplitOptions.RemoveEmptyEntries);
                foreach (var p in products)
                {
                    PriceProduct product = new PriceProduct(p);
                    Console.WriteLine(product.Name);
                    Console.WriteLine(product.Reference);
                    Console.WriteLine(product.Price);
                }
            }
        }
    }
}
